import { ref } from 'vue'
import { defineStore } from 'pinia'
import type { ShoppingCartData } from '@/typings'

export const useShoppingStore = defineStore('shopping', () => {

    const shoppingCartList = ref<Array<ShoppingCartData>>([])
    function addShoppingCartList(list: ShoppingCartData) {
        shoppingCartList.value = shoppingCartList.value.filter(item => {

            if (item.stockId == list.stockId) {
                list.num += item.num
            }
            return item.stockId != list.stockId
        })
        shoppingCartList.value.unshift(list)
        console.log(shoppingCartList.value);

    }

    function changeShoppingCartList(list: {
        selectedStatus?: boolean,
        num?: number,
        stockId?: string,
        type: number
    }) {
        if (list.type == 0) {
            shoppingCartList.value.map(item => {
                if (list.stockId == item.stockId) {
                    item.selectedStatus = list.selectedStatus
                    console.log(shoppingCartList.value);
                    console.log(list.selectedStatus);


                }
            })
        }
        if (list.type == 1) {
            shoppingCartList.value.map(item => {
                if (list.stockId == item.stockId) {
                    item.num = list.num as number
                    console.log(shoppingCartList.value);

                }
            })
        }

        if (list.type == 2) {
            shoppingCartList.value.map(item => {
                item.selectedStatus = true
            })
            console.log(shoppingCartList.value);
        }

        if (list.type == 3) {
            shoppingCartList.value.map(item => {
                item.selectedStatus = false
            })
            console.log(shoppingCartList.value);
        }


    }

    function delShopping() {
        shoppingCartList.value = shoppingCartList.value.filter(t => t.selectedStatus!=true)
    }

    return { shoppingCartList, addShoppingCartList, changeShoppingCartList ,delShopping}
})
